package com.icss.ah.fullReduce.action;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.util.MD5Util;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.j2ee.util.UUID;

/**
 * 满减/返 活动统计Action
 * @author sw
 * @date 2016-10-24
 *
 */
@Controller
@Scope("prototype")
public class FullStatisticsAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	// json串
	private Json jsonText;
	
	private String shopId;
	private String orderNo;
	private String queryName;
	private String type;
	private String queryId;
	
	/**
	 * 满减活动列表页面
	 * @return
	 */
	public String toReduceList(){
		return "toReduceList";
	}
	
	/**
	 * 查询满减活动统计列表
	 */
	public void queryReduceList(){
		request = this.getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String beginDate = request.getParameter("beginDate");
		beginDate = (beginDate == null || "".equals(beginDate)) ? "" : beginDate;
		String endDate = request.getParameter("endDate");
		endDate = (endDate == null || "".equals(endDate)) ? "" : endDate;
		String queryName = request.getParameter("queryName");
		queryName = (queryName == null || "".equals(queryName)) ? "" : queryName;
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("userCode", userCode);
			HashMap accountShop = shopService.queryAccountMap(map);
			map.put("shopId", accountShop.get("SHOP_ID") == null ? "" : accountShop.get("SHOP_ID").toString());
			map.put("corpId",corpId);	//公众号id
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("queryName", queryName);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> reduceList = new ArrayList();
			reduceList = fullStatisticsService.queryReduceList(map);
			System.out.println("reduceList--------------------------------"+reduceList);
			long reduceCount = fullStatisticsService.queryReduceCount(map);//查询总记录数
			dataGridJson.setRows(reduceList);
			dataGridJson.setTotal(reduceCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("FullStatistics|公众号id：" + corpId + "，查询满减活动统计列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullStatistics|查询满减活动统计列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 满返活动列表页面
	 * @return
	 */
	public String toReturnList(){
		return "toReturnList";
	}
	
	/**
	 * 查询满返活动统计列表
	 */
	public void queryReturnList(){
		request = this.getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String beginDate = request.getParameter("beginDate");
		beginDate = (beginDate == null || "".equals(beginDate)) ? "" : beginDate;
		String endDate = request.getParameter("endDate");
		endDate = (endDate == null || "".equals(endDate)) ? "" : endDate;
		String queryName = request.getParameter("queryName");
		queryName = (queryName == null || "".equals(queryName)) ? "" : queryName;
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("userCode", userCode);
			HashMap accountShop = shopService.queryAccountMap(map);
			map.put("shopId", accountShop.get("SHOP_ID") == null ? "" : accountShop.get("SHOP_ID").toString());
			map.put("corpId",corpId);	//公众号id
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("queryName", queryName);
			map.put("msgTypeCd", "1");
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> returnList = new ArrayList();
			returnList = fullStatisticsService.queryReturnList(map);
			System.out.println("returnList--------------------------------"+returnList);
			long returnCount = fullStatisticsService.queryReturnCount(map);//查询总记录数
			dataGridJson.setRows(returnList);
			dataGridJson.setTotal(returnCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("FullStatistics|公众号id：" + corpId + "，查询满返活动统计列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullStatistics|查询满返活动统计列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 满减/返 活动订单列表页面
	 * @return
	 */
	public String toOrderList(){
		request = this.getRequest();
		request.setAttribute("queryId", queryId);
		request.setAttribute("type", type);
		return "toOrderList";
	}
	
	/**
	 * 查询满减/返 活动订单统计列表
	 */
	public void queryOrderList(){
		request = this.getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String corpId = userInfo.getCorpId();
		String beginDate = request.getParameter("beginDate");
		beginDate = (beginDate == null || "".equals(beginDate)) ? "" : beginDate;
		String endDate = request.getParameter("endDate");
		endDate = (endDate == null || "".equals(endDate)) ? "" : endDate;
		String queryName = request.getParameter("queryName");
		queryName = (queryName == null || "".equals(queryName)) ? "" : queryName;
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("userCode", userCode);
			HashMap accountShop = shopService.queryAccountMap(map);
			map.put("queryId", queryId);
			if("reduce".equals(type)){
				map.put("yhType", 2);
			}else if("return".equals(type)){
				map.put("yhType", 3);
			}
			map.put("shopId", accountShop.get("SHOP_ID") == null ? "" : accountShop.get("SHOP_ID").toString());
			map.put("corpId",corpId);	//公众号id
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("orderNo", orderNo);
			map.put("queryName", queryName);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> orderList = new ArrayList();
			orderList = fullStatisticsService.queryOrderList(map);
			System.out.println("orderList--------------------------------"+orderList);
			long orderCount = fullStatisticsService.queryOrderCount(map);//查询总记录数
			dataGridJson.setRows(orderList);
			dataGridJson.setTotal(orderCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("FullStatistics|公众号id：" + corpId + "，查询满减活动统计列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullStatistics|查询满减活动统计列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getQueryName() {
		return queryName;
	}

	public void setQueryName(String queryName) {
		this.queryName = queryName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getQueryId() {
		return queryId;
	}

	public void setQueryId(String queryId) {
		this.queryId = queryId;
	}
	
}
